﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoMapper;
using XSTDZ.ERP.Domain.Models;
using XSTDZ.ERP.Application.DataContracts;

namespace XSTDZ.ERP.Application
{
    /// <summary>
    /// 运用程序初始化器
    /// </summary>
   public  class ApplicationServiceIniter
    {
       private static readonly ApplicationServiceIniter _instance=new ApplicationServiceIniter();

       public static ApplicationServiceIniter  Instance
       {
           get { return _instance; }
       }
       public ApplicationServiceIniter AutoMapping()
       {
           Mapper.CreateMap<UserDto, User>();
           Mapper.CreateMap<User, UserDto>();
           Mapper.CreateMap<OrgDto, Org>()
               .ForMember(o => o.OrgName, t => t.ResolveUsing(dto => dto.Name));
           Mapper.CreateMap<Org, OrgDto>()
               .ForMember(d => d.Name, o => o.ResolveUsing(org => org.OrgName));
           Mapper.CreateMap<DeptDto, Dept>();
           Mapper.CreateMap<Dept, DeptDto>();
           Mapper.CreateMap<Operation, OperationDto>()
               .ForMember(d => d.PermissionDto, o => o.ResolveUsing(m=>m.Permission));
           Mapper.CreateMap<OperationDto, Operation>()
               .ForMember(d => d.Permission, o => o.ResolveUsing(m => m.PermissionDto));
           Mapper.CreateMap<Permission, PermissionDto>();
           Mapper.CreateMap<PermissionDto, Permission>();
           Mapper.CreateMap<UiPermission, UiPermissionDto>();
           Mapper.CreateMap<UiPermissionDto, UiPermission>();

           Mapper.CreateMap<Role, RoleDto>();
           Mapper.CreateMap<RoleDto, Role>();

           Mapper.CreateMap<RefeType, RefeTypeDto>()
               .ForMember(d => d.RefeDtos, o => o.ResolveUsing(m => m.Refes));
           Mapper.CreateMap<RefeTypeDto, RefeType>()
               .ForMember(d => d.Refes, o => o.ResolveUsing(m => m.RefeDtos));
           Mapper.CreateMap<Refe, RefeDto>();
           Mapper.CreateMap<RefeDto, Refe>();

           Mapper.CreateMap<ProductTypeDto, ProductType>();
           Mapper.CreateMap<ProductType, ProductTypeDto>();

           Mapper.CreateMap<ProductInfoDto, ProductInfo>();
           Mapper.CreateMap<ProductInfo, ProductInfoDto>()
               .ForMember(dto => dto.TypeName, o => o.ResolveUsing(os => os.ProductType.TypeName));

           Mapper.CreateMap<StorehouseDto, Storehouse>();
           Mapper.CreateMap<Storehouse, StorehouseDto>()
               .ForMember(dto => dto.OrgName, o => o.ResolveUsing(os => os.Org.OrgName));
           Mapper.CreateMap<ClientInfoDto, ClientInfo>();
           Mapper.CreateMap<ClientInfo, ClientInfoDto>()
               .ForMember(dto => dto.ResponsibleDeptName, o => o.ResolveUsing(os => os.ResponsibleDept.Name))
           .ForMember(dto => dto.ResponsiblePersonName, o => o.ResolveUsing(os => os.ResponsiblePerson.Name));
           Mapper.CreateMap<ClientLinkDto, ClientLink>();
           Mapper.CreateMap<ClientLink, ClientLinkDto>()
               .ForMember(dto => dto.ClientName, o => o.ResolveUsing(os => os.ClientInfo.ClientName));

           Mapper.CreateMap<ProductInBillDto, ProductInBill>();
           Mapper.CreateMap<ProductInBill, ProductInBillDto>()
               .ForMember(dto => dto.EmployeeName, o => o.ResolveUsing(os => os.Employee.Name))
                 .ForMember(dto => dto.SenderName, o => o.ResolveUsing(os => os.Sender.Name))
                 //.ForMember(dto => dto.CreateName, o => o.ResolveUsing(os => os.Create.Name))
                  .ForMember(dto => dto.SourceOrgName, o => o.ResolveUsing(os => os.SourceOrg.OrgName))
                  .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.WareHourse.StorehouseName));
           Mapper.CreateMap<ProductInBillDetailDto, ProductInBillDetail>();
           Mapper.CreateMap<ProductInBillDetail, ProductInBillDetailDto>()
                .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.ProductInfo.ProductName));

           Mapper.CreateMap<ProductDeliveryBillDto, ProductDeliveryBill>();
           Mapper.CreateMap<ProductDeliveryBill, ProductDeliveryBillDto>()
               .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.WareHourse.StorehouseName))
               .ForMember(dto => dto.DeliveryorName, o => o.ResolveUsing(os => os.Deliveryor.Name))
               .ForMember(dto => dto.SenderName, o => o.ResolveUsing(os => os.Sender.Name))
                   .ForMember(dto => dto.DeliveryName, o => o.ResolveUsing(os => os.Delivery.OrgName))
                   .ForMember(dto => dto.AffirmorName, o => o.ResolveUsing(os => os.Affirmor.Name));
           Mapper.CreateMap<ProductDeliveryBillDetailDto, ProductDeliveryBillDetail>();
           Mapper.CreateMap<ProductDeliveryBillDetail, ProductDeliveryBillDetailDto>()
               .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.Stock.ProductInfo.ProductName));
               

           Mapper.CreateMap<ProductOutBillDto, ProductSellBill>();
           Mapper.CreateMap<ProductSellBill, ProductOutBillDto>()
                .ForMember(dto => dto.SellerName, o => o.ResolveUsing(os => os.Sellor.Name))
                 .ForMember(dto => dto.AuditorName, o => o.ResolveUsing(os => os.Auditor.Name))
                  .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.WareHourse.StorehouseName))
                  .ForMember(dto => dto.ClientName, o => o.ResolveUsing(os => os.Client.ClientName));
           Mapper.CreateMap<ProductOutBillDetailDto, ProductSellBillDetail>();
           Mapper.CreateMap<ProductSellBillDetail, ProductOutBillDetailDto>()
                .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.Stock.ProductInfo.ProductName))
                 .ForMember(dto => dto.WareHourseName, o => o.ResolveUsing(os => os.Stock.Storehouse.StorehouseName));

           Mapper.CreateMap<Stock, StockDto>()
               .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.Storehouse.StorehouseName))
                 .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.ProductInfo.ProductName));
           Mapper.CreateMap<StockDto, Stock>();


           Mapper.CreateMap<AddressAreaDto, AddressArea>();
           Mapper.CreateMap<AddressArea, AddressAreaDto>();

           Mapper.CreateMap<ProductProcedureDto, ProductProcedure>();
           Mapper.CreateMap<ProductProcedure, ProductProcedureDto>()
             .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.ProductInfo.ProductName));


           Mapper.CreateMap<ProductCheckBillDto, ProductCheckBill>();
           Mapper.CreateMap<ProductCheckBill, ProductCheckBillDto>()
                .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.WareHourse.StorehouseName))
           .ForMember(dto => dto.CheckorName, o => o.ResolveUsing(os => os.Checkor.Name));
           Mapper.CreateMap<ProductCheckBillDetailDto, ProductCheckBillDetail>();
           Mapper.CreateMap<ProductCheckBillDetail, ProductCheckBillDetailDto>()
                .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.Stock.ProductInfo.ProductName));

           Mapper.CreateMap<ReturnBillDto, ReturnBill>();
           Mapper.CreateMap<ReturnBill, ReturnBillDto>()
                .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.Storehouse.StorehouseName))
                .ForMember(dto => dto.ReturnBillorName, o => o.ResolveUsing(os => os.ReturnBillor.Name))
                .ForMember(dto => dto.DeliveryName, o => o.ResolveUsing(os => os.Delivery.OrgName))
                .ForMember(dto => dto.SenderName, o => o.ResolveUsing(os => os.Sender.Name));

           Mapper.CreateMap<ReturnBillDetailDto, ReturnBillDetail>();
           Mapper.CreateMap<ReturnBillDetail, ReturnBillDetailDto>()
               .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.Stock.ProductInfo.ProductName));

           Mapper.CreateMap<ProductReturnBillDto, ProductReturnBill>();
           Mapper.CreateMap<ProductReturnBill, ProductReturnBillDto>()
                .ForMember(dto => dto.AcceptName, o => o.ResolveUsing(os => os.Accept.Name))
                .ForMember(dto => dto.AcceptOrgName, o => o.ResolveUsing(os => os.Storehouse.Org.OrgName))
                 .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.Storehouse.StorehouseName))
                  .ForMember(dto => dto.AuditorName, o => o.ResolveUsing(os => os.Auditor.Name))
               .ForMember(dto => dto.ClientName, o => o.ResolveUsing(os => os.Client.ClientName));
           Mapper.CreateMap<ProductReturnBillDetailDto, ProductReturnBillDetail>();
           Mapper.CreateMap<ProductReturnBillDetail, ProductReturnBillDetailDto>()
               .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.Stock.ProductInfo.ProductName));

           Mapper.CreateMap<ProfitAndLossBillDto, ProfitAndLossBill>();
           Mapper.CreateMap<ProfitAndLossBill, ProfitAndLossBillDto>()
                  .ForMember(dto => dto.StorehouseName, o => o.ResolveUsing(os => os.WareHourse.StorehouseName))
                 .ForMember(dto => dto.CheckorName, o => o.ResolveUsing(os => os.Checkor.Name));
           Mapper.CreateMap<ProfitAndLossBillDetailDto, ProfitAndLossBillDetail>();
           Mapper.CreateMap<ProfitAndLossBillDetail, ProfitAndLossBillDetailDto>()
               .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.Stock.ProductInfo.ProductName));
           Mapper.CreateMap<WorkGroupDto, WorkGroup>();
           Mapper.CreateMap<WorkGroup, WorkGroupDto>()
                .ForMember(dto => dto.Name, o => o.ResolveUsing(os => os.User.Name));


           Mapper.CreateMap<ProductAllotDto, ProductAllot>();
           Mapper.CreateMap<ProductAllot, ProductAllotDto>()
               .ForMember(dto => dto.FromClientName, o => o.ResolveUsing(os => os.ClientInfo.ClientName))
           .ForMember(dto => dto.ToClientName, o => o.ResolveUsing(os => os.ClientInfo1.ClientName))
            .ForMember(dto => dto.AllotorName, o => o.ResolveUsing(os => os.Allotor.Name))
             .ForMember(dto => dto.AllotOrgName, o => o.ResolveUsing(os => os.AllotOrg.OrgName));
           Mapper.CreateMap<ProductAllotDetailDto, ProductAllotDetail>();
           Mapper.CreateMap<ProductAllotDetail, ProductAllotDetailDto>()
                .ForMember(dto => dto.ProductName, o => o.ResolveUsing(os => os.ProductInfo.ProductName));

           Mapper.CreateMap<WagesBillDto, WagesBill>();
           Mapper.CreateMap<WagesBill, WagesBillDto>();
              // .ForMember(dto => dto.WorkGroupName, o => o.ResolveUsing(os => os.WorkGroup.GroupName));

           Mapper.CreateMap<WagesBillDetailDto, WagesBillDetail>();
           Mapper.CreateMap<WagesBillDetail, WagesBillDetailDto>();


           Mapper.CreateMap<PieceDto, Piece>();
           Mapper.CreateMap<Piece, PieceDto>()
                .ForMember(dto => dto.WorkGroupName, o => o.ResolveUsing(os => os.WorkGroup.GroupName))
                .ForMember(dto => dto.UserName, o => o.ResolveUsing(os => os.WorkGroup.User.Name))
                .ForMember(dto => dto.ProductProcedureName, o => o.ResolveUsing(os => os.ProductProcedure.ProcedureName));

           Mapper.CreateMap<ReceivablePayableBillDto, ReceivablePayableBill>();
           Mapper.CreateMap<ReceivablePayableBill, ReceivablePayableBillDto>()
                .ForMember(dto => dto.ClientName, o => o.ResolveUsing(os => os.ClientInfo.ClientName));
           Mapper.CreateMap<ReceivablePayableRecordDto, ReceivablePayableRecord>();
           Mapper.CreateMap<ReceivablePayableRecord, ReceivablePayableRecordDto>();

           Mapper.CreateMap<UsualFinanceDto, UsualFinance>();
           Mapper.CreateMap<UsualFinance, UsualFinanceDto>()
               .ForMember(dto => dto.OrgName, o => o.ResolveUsing(os => os.Org.OrgName));
           Mapper.CreateMap<UsualFinanceDetailDto, UsualFinanceDetail>();
           Mapper.CreateMap<UsualFinanceDetail, UsualFinanceDetailDto>()
               .ForMember(dto => dto.OperatorName, o => o.ResolveUsing(os => os.Operator.Name))
               .ForMember(dto => dto.RefeName, o => o.ResolveUsing(os => os.Refe.RefeName));

           Mapper.CreateMap<RemittanceRecordDto, RemittanceRecord>();
           Mapper.CreateMap<RemittanceRecord, RemittanceRecordDto>()
               .ForMember(dto => dto.RemittancePeoperName, o => o.ResolveUsing(os => os.RemittancePeoper.Name));
           Mapper.CreateMap<DataBackDto, DataBack>()
               .ForMember(dto =>dto.Id, o => o.ResolveUsing(os =>Guid.NewGuid()))
                .ForMember(dto => dto.DataBackID, o => o.ResolveUsing(os => os.Id));
           Mapper.CreateMap<DataBack, DataBackDto>()
               .ForMember(dto => dto.Id, o => o.ResolveUsing(os => os.DataBackID));

           return this;
       }
      
      
    }
}
